<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');
jimport('joomla.utilities.date');

class iJoomla_SurveysModelNewSurvey extends JModel{	
	
	function getSurveys(){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('title, ordering');
		$query->from('#__ijoomla_surveys_surveys');
		$query->order("ordering ASC");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function getSurveyValues(){
		$id = JRequest::getVar("id", "0");
		if($id != "0"){
			$db =& JFactory::getDBO();		
			$query = $db->getQuery(true);
			$query->clear();		
			$query->select('*');
			$query->from('#__ijoomla_surveys_surveys');
			$query->where("id=".$id);
			$db->setQuery($query);		
			$db->query();
			$result = $db->loadAssocList();		
			return $result;
		}
		else{
			return "";
		}
	}
	
	function save(){
		$db =& JFactory::getDBO();		
		$title = JRequest::getVar('title', "");
		$alias = JRequest::getVar('alias', "");
		$table =& $this->getTable();		
		$return = array();
		$user =& JFactory::getUser();
        $gid = $user->id;
		$id = JRequest::getVar("id", "0");
		
		if($alias != ""){
			$alias = strtolower($alias);
			$alias = str_replace(" ", "-", $alias);
			$alias = str_replace("&", "a", $alias);
			$alias = str_replace("@", "", $alias);
			$alias = str_replace("'", "", $alias);
			$alias = str_replace('"', "", $alias);
			$alias = str_replace('?', "", $alias);
			$alias = str_replace('/', "", $alias);
			$alias = str_replace('\\', "", $alias);
			$alias = $alias;
			JRequest::setVar('alias', $alias);
		}
		else{
			$alias = strtolower($title);
			$alias = str_replace(" ", "-", $alias);
			$alias = str_replace("&", "a", $alias);
			$alias = str_replace("@", "", $alias);
			$alias = str_replace("'", "", $alias);
			$alias = str_replace('"', "", $alias);
			$alias = str_replace('?', "", $alias);
			$alias = str_replace('/', "", $alias);
			$alias = str_replace('\\', "", $alias);
			$alias = $alias;			
			JRequest::setVar('alias', $alias);
		}
		
		$all_dates = JRequest::get('post',JREQUEST_ALLOWRAW);
		
		if($id == "0"){		
			$ordering = $this->getMaxOrdering();
			$all_dates["ordering"] = $ordering;
		}
		else{
			unset($all_dates["ordering"]);
		}
		
		JRequest::setVar('user_id', $gid);		
		$params = $this->getParams();
		$format = $params->general_date;
		
		if(!isset($all_dates["imagelist"])){
			$all_dates["imagelist"] = "";
		}
			
		// Bind the form fields to the news table		
		if (!$table->bind($all_dates)){
			echo $this->_db->getErrorMsg(); exit;
			$this->setError($this->_db->getErrorMsg());
			$return["0"] = false;
		}
		// Make sure the news record is valid
		if (!$table->check()){
			echo $this->_db->getErrorMsg(); exit;
			$this->setError($this->_db->getErrorMsg());
			$return["0"] = false;
		}
		// Store the web link table to the database
		if (!$table->store()){
			echo $this->_db->getErrorMsg(); exit;
			$this->setError( $this->_db->getErrorMsg() );
			$return["0"] = false;
		}		
		
		$return["0"] = true;
		$return["1"] = $table->id;
		
		return $return;
	}
	
	function getMaxOrdering(){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('max(ordering)');
		$query->from('#__ijoomla_surveys_surveys');
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadResult();

		return (int)($result)+1;
	}
	
	function getParams(){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('params');
		$query->from('#__extensions');
		$query->where("element='com_surveys'");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadResult();
		$array = json_decode($result);
		return $array;
	}
	
}

?>